/*
 * [Ronny Abraham]
 * [ronnya@gmail.com]
 * CS530:DUI, Assignment [#1]
 */

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/*
 * ActorsView
 */
public class ActorsView 
{
	ActorsView(ActorsTableModel actorsTableModel, ActorsController controller)
	{
		itsActorsTableModel = actorsTableModel;
		itsActorsController = controller;
		init();
	}
	
	private void init()
	{
		// actors panel
		itsActorsPanel = new JPanel();
		itsActorsPanel.setLayout(new BoxLayout(itsActorsPanel, BoxLayout.PAGE_AXIS));
		// actors table
		itsActorsTable = new JTable(itsActorsTableModel);
		itsActorsTable.setRowSelectionAllowed(true);
		itsActorsTable.setColumnSelectionAllowed(false);
		itsActorsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		itsRowSelectionListener = new RowSelectionListener(itsActorsTable, itsActorsController);
		itsActorsTable.getSelectionModel().addListSelectionListener(itsRowSelectionListener);
		itsActorsTable.getColumnModel().getSelectionModel().addListSelectionListener(itsRowSelectionListener);
		itsActorsTable.addMouseListener(new TableMouseListener(this));
	    // creating a scroll pane for the table
		JScrollPane scrollPane = new JScrollPane(itsActorsTable);
		itsActorsPanel.add(scrollPane);
	    // action buttons (New, Modify, Remove)
		
		// 'New' button
		JPanel actionBtnsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
	    JButton addActorBtn = new JButton("New");
	    addActorBtn.addActionListener(new ActionListener()
		{
	    	// listener for newing an actor
			public void actionPerformed(ActionEvent event)
			{
				itsActorsController.addActor();
			}
		});
	    // 'Remove' button
	    rmvActorBtn = new JButton("Remove");
	    rmvActorBtn.setEnabled(false);
	    rmvActorBtn.addActionListener(new ActionListener()
		{
	    	// listener to clicking "Remove"
			public void actionPerformed(ActionEvent event)
			{
				int index = itsActorsTable.getSelectedRow();
				if(index > -1)
				{
					itsActorsTableModel.removeActor(index);
					itsActorsTableModel.fireTableRowsDeleted(index, index);
					selectRowAfterDeletion(index);
				}
			}
			// making sure the right row is selected after deleting
			private void selectRowAfterDeletion(int deletedIndex)
			{
				if(itsActorsTableModel.getRowCount() == 0)
				{
					// empty table, nothing gets selected
					itsActorsController.tableEmpty();
					edtActorBtn.setEnabled(false);
					rmvActorBtn.setEnabled(false);
					return;
				}
				else if(deletedIndex == 0)
				{
					// first row was deleted, select the (new) first row
					itsActorsTable.setRowSelectionInterval(0, 0);
					
				}
				else if(deletedIndex == itsActorsTableModel.getRowCount())
				{
					// last row deleted, select the (new) last row
					itsActorsTable.setRowSelectionInterval(deletedIndex - 1, deletedIndex - 1);
				}
				else
				{
					// middle row deleted, select the next row
					itsActorsTable.setRowSelectionInterval(deletedIndex, deletedIndex);
				}
			}
		});
	    // 'Edit' button
	    edtActorBtn = new JButton("Edit");
	    edtActorBtn.setEnabled(false);
	    edtActorBtn.addActionListener(new ActionListener()
		{
	    	// listener for clicking 'Edit'
			public void actionPerformed(ActionEvent event)
			{
				int index = itsActorsTable.getSelectedRow();
				if(index > -1)
				{
					itsActorsController.editActor(index);
				}
			}
		});
	    
	    // add the action buttons to teh panel
	    actionBtnsPanel.add(addActorBtn);
	    actionBtnsPanel.add(edtActorBtn);
	    actionBtnsPanel.add(rmvActorBtn);
	    itsActorsPanel.add(actionBtnsPanel);
		
	}

	public ActorsController itsActorsController;
	public JPanel itsActorsPanel;
	public JTable itsActorsTable;
	public JButton edtActorBtn, rmvActorBtn;
	private ActorsTableModel itsActorsTableModel;
	private RowSelectionListener itsRowSelectionListener;
}

/*
 * RowSelectionListener
 * listens to row selection, calling the controller (which will change the preview)
 */
class RowSelectionListener implements ListSelectionListener
{
	RowSelectionListener(JTable table, ActorsController controller)
	{
		itsJTable = table;
		itsActorsController = controller;
	}
	@Override
	public void valueChanged(ListSelectionEvent e)
	{
		 if(!e.getValueIsAdjusting())
		 {
			 int row = itsJTable.getSelectedRow();
			itsActorsController.rowSelected(row);
		 }
	}
	
	private ActorsController itsActorsController;
	private JTable itsJTable;
}

/*
 * I added this just for fun
 * double clicking on a row will open the edit actor dialog
 */
class TableMouseListener implements  MouseListener
{
	public TableMouseListener(ActorsView view)
	{
		itsActorsView = view;
	}
	
	@Override
	public void mouseClicked(MouseEvent e)
	{
		if (e.getComponent().isEnabled() && e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2)
        {
			int index = itsActorsView.itsActorsTable.getSelectedRow();
			if(index > -1)
			{
				itsActorsView.itsActorsController.editActor(index);
			}
        }
	}

	@Override
	public void mousePressed(MouseEvent e)
	{	
	}

	@Override
	public void mouseReleased(MouseEvent e)
	{	
	}

	@Override
	public void mouseEntered(MouseEvent e)
	{	
	}

	@Override
	public void mouseExited(MouseEvent e)
	{	
	}
	
	private ActorsView itsActorsView;
}